package org.dfl.plugin;

import java.awt.BorderLayout;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.UIManager;

import org.dfl.log.Logger;
import org.dfl.plugin.dialogs.TavernaProcesorPropertiesListener;
import org.dfl.plugin.views.DflPluginConsole;
import org.dfl.plugin.views.palette.TavernaTreeLoader;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.embl.ebi.escience.scufl.ScuflModel;
import org.embl.ebi.escience.scuflui.shared.UIUtils;
import org.embl.ebi.escience.scuflui.shared.UIUtils.FrameCreator;
import org.embl.ebi.escience.scuflui.spi.UIComponentSPI;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;


/**
 * The activator class controls the plug-in life cycle
 */
public class DflPlugin extends AbstractUIPlugin {
	// The plug-in ID
	public static final String PLUGIN_ID = "org.dfl.plugin";

	// The shared instance
	private static DflPlugin plugin;
	
	/** default files extention that this plugin editor understands **/
	public static final String DEFAULT_EXTENSION = ".dfl";

	// preferences
	private static final String DOT_LOCATION_PREFERENCE = "dotLocationPreference";

	public static TavernaProcesorPropertiesListener tavernaProcesorPropertiesListener;

	private DflPluginConsole console;
	/**
	 * The constructor
	 */
	public DflPlugin() {		
		plugin = this;
		// run taverna loader
		TavernaTreeLoader.getInstance().run();
		
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e1) {}
		
		
		// make w wrapper for swing - all popups must be shown as swt dialogs
		UIUtils.DEFAULT_FRAME_CREATOR = new FrameCreator() {
/*			public void createFrame(ScuflModel targetModel,ScuflUIComponent targetComponent,
					int posX, int posY, int sizeX, int sizeY) {
				JFrame newFrame = new JFrame(targetComponent.getName());
				newFrame.getContentPane().setLayout(new BorderLayout());
				newFrame.getContentPane().add(
						new JScrollPane((JComponent) targetComponent),
						BorderLayout.CENTER);
				newFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
				newFrame.setSize(sizeX, sizeY);
				newFrame.setLocation(posX, posY);
				
				newFrame.setVisible(true);
				newFrame.setAlwaysOnTop(true);
				if(tavernaProcesorPropertiesListener!=null){
					newFrame.addWindowListener(tavernaProcesorPropertiesListener);
					tavernaProcesorPropertiesListener = null;
				}
			} */

			public void createFrame(ScuflModel targetModel, UIComponentSPI targetComponent,
					int posX, int posY, int sizeX, int sizeY) {
				try {
					JFrame newFrame = new JFrame(targetComponent.getName());
					newFrame.getContentPane().setLayout(new BorderLayout());
					newFrame.getContentPane().add(
							new JScrollPane((JComponent) targetComponent),
							BorderLayout.CENTER);
					newFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
					newFrame.setSize(sizeX, sizeY);
					newFrame.setLocation(posX, posY);
					
					newFrame.setVisible(true);
					newFrame.setAlwaysOnTop(true);
					if(tavernaProcesorPropertiesListener!=null){
						newFrame.addWindowListener(tavernaProcesorPropertiesListener);
						tavernaProcesorPropertiesListener = null;
					}
				} catch (Exception ex) {
					Logger.logException(ex);
				}
			}
		}; 
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}

	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static DflPlugin getDefault() {
		return plugin;
	}

	/**
	 * Returns an image descriptor for the image file at the given
	 * plug-in relative path
	 *
	 * @param path the path
	 * @return the image descriptor
	 */
	public static ImageDescriptor getImageDescriptor(String path) {
		return imageDescriptorFromPlugin(PLUGIN_ID, path);
	}
	
	/**
	 * Returns URL of specific file at the given plug-in relative path
	 * @param filePath the path
	 * @return the URL relative to plug-in path
	 */
	@SuppressWarnings({ "restriction", "deprecation" })
	public static URL getURLFromPlugin(String filePath) {
		// if the bundle is not ready then there is no image
		Bundle bundle = Platform.getBundle(PLUGIN_ID);

		// look for the image (this will check both the plugin and fragment folders
		URL fullPathString = Platform.find(bundle, new Path(filePath));
		if (fullPathString == null) {
			try {
				fullPathString = new URL(filePath);
			} catch (MalformedURLException e) {
				return null;
			}
		}

		return fullPathString;
	}

	public void setDotLocationPreference(String dotLocationPreference) {
		getPreferenceStore().setValue(DOT_LOCATION_PREFERENCE, dotLocationPreference);
	}

	public String getDotLocationPreference() {
		return getPreferenceStore().getString(DOT_LOCATION_PREFERENCE);
	}

	public DflPluginConsole getConsole() {
		if(console==null){
			console = new DflPluginConsole();
			console.show();
		}
		return console;
	}
}
